<?php

namespace app\components;

use app\models\user\IdentificationList;
use Yii;

//use yii\web\User;
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of User
 *
 * @author shuai
 */
class WebUser extends \yii\web\User
{
    public function __construct($config = [])
    {
        $this->loadUser();
        parent::__construct($config);
    }

    private function loadUser()
    {
        $token=null;
        $cookie=Yii::$app->request->getCookies();
        $cookietoken=$cookie->get("token");
        if($cookietoken){
            $token=$cookietoken->value;
        }
        $header=Yii::$app->request->getHeaders();
        if($header->get("token")){
            $token=$header->get("token");
        }
        if(!$token){
            return;
        }
        $this->id=IdentificationList::getUid($token);
        $this->isGuest=empty($this->id);
        $this->identity=new $this->identityClass;
        $this->identity->id= $this->id;
    }
    public function getId(){
        return $this->id;
    }

    public $id=0;

    public $isGuest=true;

    /**
     * 小图头像
     */
    const AvatarMin = 2;
    /**
     * 正常大小
     */
    const AvatarYuantu = 1;


    // public $identityClass = 'app\models\User';
    public $identityClass = 'app\components\SessionUser';

    public function getCacheModel()
    {
        $one = \app\models\User::find()->where([
            'id' => $this->getId()
        ])->select([
            'id', 'username', 'avatar', 'phone', 'is_company', 'is_factory', 'is_yingpin', 'jiguang_id', 'jiguang_pass'
        ])->one();//->toArray();
        if (!$one) {
            return null;
        }
        $data = [
            'id' => $one['id'],
            'name' => $one['username'],
            'head' => $one['avatar'],
            'phone' => $one['phone'],
            'is_company' => $one['is_company'],
            'is_factory' => $one['is_factory'],
            'is_yingpin' => $one['is_yingpin'],
            'is_yingpin' => $one['is_yingpin'],
            'jiguang_id' => $one['jiguang_id'],
            'jiguang_pass' => $one['jiguang_pass']
        ];
        return $data;
    }

    public function getModel($select = null)
    {
        if ($select == null) {
            $select = [
                'id', 'username', 'avatar',
                'phone', 'is_company',
                'is_factory', 'is_yingpin',
                'jiguang_id', 'jiguang_pass', 'type'
            ];
        }
        $one = \app\models\User::find()->where([
            'id' => $this->getId()
        ])->select($select)->one();//->toArray();
        if (!$one) {
            return null;
        }
        return $one;
    }


    /**
     * 返回是否为登录状态
     */


    public function get_name()
    {
        if (Yii::$app->user->isGuest) {
            return '';
        }
        return Yii::$app->user->identity->username;
    }

    public function openId()
    {
        if (Yii::$app->user->isGuest) {
            return '';
        }
        return Yii::$app->user->identity->open_id;
    }
/*
    public function getId()
    {

        if ($this->isGuest) {
            return 0;
        }
        return parent::getId();
    }*/




    public function get_login_url()
    {
        if (Lcms::get_browser() == Lcms::APP_weixin) {
            $login = new Weixin();
            return $login->getLoginHref();
        }
    }


    /**
     * 获取当前用户头像
     * p3.music.126.net/orLdcpB-NlHXBLRv7VNsXg==/5687773650588617.jpg?param=40y40
     * 注意后面的param参数，可以控制图片大小。
     */
    public function getAvatar()
    {
        if ($this->isGuest || empty($this->identity->avatar)) {
            return Lcms::getWeb() . \app\models\User::defaultAvatar;
        } else {
            $url = $this->identity->avatar;

            return $url;
        }
    }

    /**
     * 重定向到登录
     */
    public function godenglu()
    {
        Yii::$app->response->redirect(
            Yii::$app->user->get_login_url()
        );
        Yii::$app->end();
    }

    /**
     *
     */

}
